//
//  FKProductParametersPopView.m
//  HHShopping
//
//  Created by Macbook Pro on 2018/8/26.
//  Copyright © 2018年 Macbook Pro - Macbook Pro. All rights reserved.
//

#import "FKProductParametersPopView.h"
#import "FKProductParameterCell.h"

@interface FKProductParametersPopView() <UITableViewDataSource, UITableViewDelegate>
/*底部弹出视图*/
@property(nonatomic, weak) UIControl *coverView;
@property(nonatomic, weak) UIView *whiteView;
@property(nonatomic, weak) UILabel *titleLabel;
@property(nonatomic, weak) UITableView *tableView;

@end

@implementation FKProductParametersPopView

- (NSArray *)datas
{
    if (!_datas) {
        _datas = [NSArray array];
    }
    return _datas;
}

+ (instancetype)parameterView
{
    FKProductParametersPopView *view = [[FKProductParametersPopView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    return view;
}

- (void)show
{
    [self.tableView reloadData];
    
    UIWindow *keyWindow  = [UIApplication sharedApplication].delegate.window;
    [keyWindow addSubview:self];
    [UIView animateWithDuration:0.25 animations:^{
        self.coverView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
        self.whiteView.fk_top = fkScreenH - self.whiteView.fk_height;
    }];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setupSubviews];
    }
    return self;
}

- (void)setupSubviews
{
    UIControl *coverView = [[UIControl alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [self addSubview:coverView];
    self.coverView = coverView;
    coverView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.0];
    [coverView addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
    coverView.userInteractionEnabled = YES;
    
    CGFloat factor = 880.f / 1334.f;
    UIView *whiteView = [[UIView alloc] initWithFrame:CGRectMake(0, fkScreenH, fkScreenW, fkScreenH * factor)];
    [self addSubview:whiteView];
    self.whiteView = whiteView;
    whiteView.backgroundColor = [UIColor whiteColor];
    
    UILabel *titleLabel = [UILabel fk_labelWithFont:fk_adjustFont(16) textColor:[UIColor blackColor] textAlignment:NSTextAlignmentCenter text:@"产品参数"];
    [whiteView addSubview:titleLabel];
    self.titleLabel = titleLabel;
    titleLabel.frame = CGRectMake(0, 20, fkScreenW, 24);
    
    UIButton *completeBtn = [UIButton fk_btnWithTarget:self action:@selector(dismiss) font:fk_adjustFont(16) normalTitle:@"确定" normalTitleColor:[UIColor whiteColor] selectedTitle:nil selectedTitleColor:nil normalImg:nil selectedImg:nil bgImg:[UIImage fk_imageWithColor:fkMainColor] selectedBgImg:nil];
    [whiteView addSubview:completeBtn];
    CGFloat completeBtnH = 48;
    CGFloat completeBtnY = whiteView.fk_height - completeBtnH - 6 - fkOffsetBottom;
    completeBtn.frame = CGRectMake(0, completeBtnY, fkScreenW, completeBtnH);
    
    UITableView *tableView = [[UITableView alloc] init];
    [whiteView addSubview:tableView];
    self.tableView = tableView;
    tableView.dataSource = self;
    tableView.delegate = self;
    tableView.tableFooterView = [[UIView alloc] init];
    tableView.alwaysBounceVertical = false;
    CGFloat tableH = completeBtnY - 6 - titleLabel.fk_bottom;
    tableView.frame = CGRectMake(0, titleLabel.fk_bottom, fkScreenW, tableH);
}

#pragma mark - target
- (void)dismiss
{
    [self resignFirstResponder];
    [UIView animateWithDuration:0.25 animations:^{
        self.coverView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.0];
        self.whiteView.fk_top = fkScreenH;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.datas.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FKProductParameterCell *cell = [FKProductParameterCell cellWith:tableView];
    cell.data = self.datas[indexPath.row];
    return cell;
}


#pragma mark - UITableViewDelegate



@end

